package com.lanyuan.controller.app;

import com.baidu.aip.util.Util;
import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.app.DiagnosisAnswerFormMap;
import com.lanyuan.entity.app.TongueStandardFormMap;
import com.lanyuan.exception.SystemException;
import com.lanyuan.mapper.app.DiagnosisMapper;
import com.lanyuan.mapper.app.TongueStandardMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.BaiduFaceAi;
import com.lanyuan.util.BaiduImageSearch;
import com.lanyuan.util.Common;
import com.lanyuan.util.DateUtil;
import com.lanyuan.util.baidu.Base64Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/app/tongue/")
public class TongueStandardController extends BaseController {
    @Inject
    private TongueStandardMapper tongueStandardMapper;

    @RequestMapping("standardList")
    public String standardList(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/diagnosis/tongue/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
        TongueStandardFormMap formMap = getFormMap(TongueStandardFormMap.class);
        formMap = toFormMap(formMap, pageNow, pageSize, formMap.getStr("orderby"));
        formMap.put("column", column);
        formMap.put("sort", sort);
        PageView pageView = getPageView(formMap);
        pageView.setRecords(tongueStandardMapper.findPage(formMap)); // 不调用默认分页,调用自已的mapper中findPage
        return pageView;
    }





    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/diagnosis/tongue/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @SystemLog(module = "配置管理", methods = "新增舌诊标准图片") // 凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    public String addEntity() {
        try {
            TongueStandardFormMap formMap = getFormMap(TongueStandardFormMap.class);
            formMap.put("create_time", DateUtil.formatDateTime());
            tongueStandardMapper.addEntity(formMap); // 新增后返回新增信息
//            String breif ="{\"id\":"+formMap.getLong ("id")+",\"title\":"+formMap.getStr("title")+"}";
//            BaiduImageSearch.add("E:\\idea\\imageai\\target\\imageai"+formMap.getStr("path"),breif);
           System.out.println("imagePath:"+formMap.getStr("path"));
//            String base64 = Base64Util.encode(Util.readFileByBytes( formMap.getStr("path")));
            String base64 = Base64Util.encode(Util.readFileByBytes( formMap.getStr("path")));
            String face_token = BaiduFaceAi.addFace(base64, "painting", formMap.getLong("id")+"", formMap.getStr("title"));
            formMap.put("face_token",face_token);
            tongueStandardMapper.editEntity(formMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException("添加配置异常");
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            TongueStandardFormMap formMap = tongueStandardMapper.findOne("id", id, TongueStandardFormMap.class);

            model.addAttribute("tongueStandard", formMap);

        }
        return Common.BACKGROUND_PATH + "/diagnosis/tongue/edit";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "修改问诊题目信息") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity()  {
        String code = "success";
        try {
            TongueStandardFormMap formMap = getFormMap(TongueStandardFormMap.class, false);
            tongueStandardMapper.editEntity(formMap);
//            String base64 = Base64Util.encode(Util.readFileByBytes( formMap.getStr("path")));
            String base64 = Base64Util.encode(Util.readFileByBytes(formMap.getStr("path")));
            String face_token = BaiduFaceAi.faceUpdate(base64, "painting", formMap.getStr("id"), formMap.getStr("title"));
            formMap.put("face_token", face_token);
            tongueStandardMapper.editEntity(formMap);
        }catch (Exception e){
            e.printStackTrace();
            throw new SystemException(e.getMessage());
        }
        return code;
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "问诊管理-删除题目") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            TongueStandardFormMap id1 = tongueStandardMapper.findOne("id", id, TongueStandardFormMap.class);
            BaiduFaceAi.faceDelete(id1.getStr("face_token"),"painting",id);
            tongueStandardMapper.deleteByAttribute("id", id, TongueStandardFormMap.class);

        }
        return "success";
    }

    // 更改盟友展业状态
    @RequestMapping("editStatus")
    @ResponseBody
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "更改问诊题目状态") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editStatus() throws Exception {
        String id = getPara("id");
        String status = getPara("status");
        TongueStandardFormMap userFormMap = getFormMap(TongueStandardFormMap.class);
        userFormMap.set("id", id);
        userFormMap.set("status", status);
        tongueStandardMapper.editEntity(userFormMap);
        return "success";
    }



}
